package com.fishery.processing.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.entity.Result;
import com.fishery.processing.dao.InputDao;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Input;
import com.fishery.processing.entity.vo.CraftVo;
import com.fishery.processing.entity.vo.InputVo;
import com.fishery.processing.service.CraftService;
import com.fishery.processing.service.InputService;
import com.fishery.processing.util.ConvertUtil;
import com.fishery.processing.util.UpdateProxy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Api(tags = "投入品相关接口")
@CrossOrigin
@RestController
@RequestMapping("/processingFactory/input")
public class InputController {

    @Autowired
    private InputService inputService;

    @Autowired
    private UpdateProxy updateProxy;

    @Autowired
    private ConvertUtil convertUtil;

    @PostMapping("/")
    @ApiOperation("新增投入品")
    public Result createInput(@Valid @RequestBody @ApiParam(required = true,value = "inputVo") InputVo inputVo){
        return inputService.createInput(convertUtil.convert(inputVo,Input.class)) == 1
                ? Result.success("新增成功") : Result.success("新增失败");
    }

    @PutMapping("/")
    @ApiOperation("修改投入品信息")
    public Result updateInput(@Valid @RequestBody @ApiParam(required = true,value = "inputVo") InputVo inputVo){
        Input input = inputService.searchInputById(inputVo.getId());
        BeanUtils.copyProperties(inputVo,input);
        //使用反射对修改操作进行检查
        return ((InputService)(updateProxy.bind(inputService).getProxyInstance())).updateInput(input) == 1
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除投入品")
    public Result deleteInput(@PathVariable(value = "id") String inputId) {
        return inputService.deleteInput(inputId) == 1
                ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据投入品id获取投入品信息")
    public Result searchInputById(@PathVariable(value = "id") String inputId){
        return Result.success().data(convertUtil.convert(inputService.searchInputById(inputId),InputVo.class));
    }

    @GetMapping("/craftInput/{id}/{page}/{size}")
    @ApiOperation("分页查找工艺的投入品信息")
    public Result searchInputsOfCraft(@PathVariable(value = "id") String craftId
                                        ,@PathVariable(value = "page") Integer page
                                        ,@PathVariable(value = "size") Integer size){
        return Result.success()
                .data(convertUtil.convert(inputService.searchInputsOfCraft(craftId, page, size),InputVo.class));
    }

    @GetMapping("/craftInput/{id}")
    @ApiOperation("查找工艺的投入品信息")
    public Result searchInputsOfCraft(@PathVariable(value = "id") String craftId){
        return Result.success()
                .data(convertUtil.convert(inputService.searchInputsOfCraft(craftId),InputVo.class));
    }

    @GetMapping("/factoryInput/{id}/{page}/{size}")
    @ApiOperation("分页查找加工厂的投入品")
    public Result searchInputsOfFactory(@PathVariable(value = "id") String factoryId
                                        ,@PathVariable(value = "page") Integer page
                                        ,@PathVariable(value = "size") Integer size){
        return Result.success()
                .data(convertUtil.convert(inputService.searchInputsOfFactory(factoryId, page, size),InputVo.class));
    }

    @GetMapping("/factoryInput/{id}")
    @ApiOperation("查找加工厂的投入品")
    public Result searchInputsOfFactory(@PathVariable(value = "id") String factoryId){
        return Result.success()
                .data(convertUtil.convert(inputService.searchInputsOfFactory(factoryId),InputVo.class));
    }
}

